package DiffieHellman;

import DiffieHellman.TestSM2.TestSM2;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.ec.CustomNamedCurves;
import org.bouncycastle.math.ec.ECPoint;

import java.math.BigInteger;

public class ECDHexchange {

//    private static final X9ECParameters ecc = new TestSM2().createParameters();
    private static final X9ECParameters ecc = CustomNamedCurves.getByName("sm2p256v1");

    // 椭圆曲线上的基点
    private final ECPoint g;
    // 用户 A 的公钥
    private ECPoint pA;
    // 用户 B 的公钥
    private ECPoint pB;

    private ECPoint k;

    public ECDHexchange() {
        g = ecc.getG();
    }

    public boolean ECDH(BigInteger skA, BigInteger skB) {
        pA = g.multiply(skA);
        pB = g.multiply(skB);

        ECPoint keyA = pA.multiply(skB);
        ECPoint keyB = pB.multiply(skA);

        k = keyA;

        return keyA.equals(keyB);
    }

    public ECPoint getpA() {
        return pA;
    }

    public ECPoint getpB() {
        return pB;
    }

    public ECPoint getK() {
        return k;
    }

}
